package cn.kevinkun.Box2D;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.jbox2d.common.Vec2;

import com.google.appinventor.components.runtime.Form;
import com.google.appinventor.components.runtime.ReplForm;
import com.google.appinventor.components.runtime.util.FileUtil;
import com.google.appinventor.components.runtime.util.YailList;

import android.os.Build;

public class BoxUtil {
	private static int ratio = 50;

	private static float DEGTORAD = 0.0174532925199432957f;
	private static float RADTODEG = 57.295779513082320876f;

	public static String getAbsolutePath(Form form, String filename) {
		filename = filename.replaceAll("file://", "");
		String result = "";

		if (filename.startsWith("//")) {
			filename = filename.substring(2);
			if (ReplForm.class.isInstance(form)) {
				String asd = form.$context().getExternalFilesDir(null).getPath();
				if (Build.VERSION.SDK_INT >= 29) {
					result = asd + "/assets/" + filename;
				} else {
					result = asd + "/AppInventor/assets/" + filename;
				}
			} else {
				result = form.$context().getFilesDir().getPath() + "/" + filename;
				try {
					InputStream in = form.openAsset(filename);
					FileUtil.writeStreamToFile(in, result);
				} catch (IOException localIOException) {

				}
			}
		} else if (filename.startsWith("/storage/emulated/")) {
			result = filename;
		} else if (filename.startsWith("/")) {
			result = form.$context().getExternalFilesDir(null).getPath() + filename;
		} else {
			result = form.$context().getExternalFilesDir(null).getPath() + "/" + filename;
		}
		return result;
	}

	// this function from AI2 source code in Canvas.java
	public static float[][] parsePointList(YailList pointList) throws IllegalArgumentException {
		if (pointList == null || pointList.size() == 0) {
			throw new IllegalArgumentException();
		}
		float[][] points = new float[pointList.size()][2];
		int index = 0;
		YailList pointYailList;
		for (Object pointObject : pointList.toArray()) {
			if (pointObject instanceof YailList) {
				pointYailList = (YailList) pointObject;
				if (pointYailList.size() == 2) {
					try {
						points[index][0] = Float.parseFloat(pointYailList.getString(0));
						points[index][1] = Float.parseFloat(pointYailList.getString(1));
						index++;
					} catch (NullPointerException e) {
						throw new IllegalArgumentException(e.fillInStackTrace());
					} catch (NumberFormatException e) {
						throw new IllegalArgumentException(e.fillInStackTrace());
					}
				} else {
					throw new IllegalArgumentException("length of item YailList(" + index + ") is not 2");
				}
			} else {
				throw new IllegalArgumentException("item(" + index + ") in YailList is not a YailList");
			}
		}
		return points;
	}

	public static Vec2 positionToVec2(List<Integer> list) {
		return new Vec2(list.get(0), list.get(1));
	}

	public static float radiansToDegrees(float radians) {
		return radians * RADTODEG;
	}

	public static float degreesToRadians(float degrees) {
		return degrees * DEGTORAD;
	}

	public static float M2P(float meters) {
		return meters * ratio;
	}

	public static float P2M(float pixels) {
		return pixels / ratio;
	}

	public static YailList vec2ToList(Vec2 vec) {
		return YailList.makeList(new Object[] { vec.x, vec.y });
	}

	public static Vec2 pixelToVec2(float x, float y) {
		return new Vec2(x / ratio, y / ratio);
	}
}
